package cn.xscrum.vtsp.wxapi;

import cn.xscrum.common.config.Global;
import cn.xscrum.common.utils.DateUtils;
import cn.xscrum.vtsp.domain.*;
import cn.xscrum.vtsp.service.*;
import cn.xscrum.vtsp.vo.ServiceRecord;
import cn.xscrum.vtsp.vo.VtspEnrollVo;
import cn.xscrum.system.domain.SysDictData;
import cn.xscrum.system.service.ISysDictDataService;
import cn.xscrum.vtsp.domain.VtspAreas;
import cn.xscrum.vtsp.service.IVtspAreasService;
import cn.xscrum.wxapp.authc.ApiToken;
import cn.xscrum.wxapp.common.ApiBaseController;
import cn.xscrum.wxapp.common.ApiResult;
import cn.xscrum.wxapp.model.UserModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Api(tags = "微信接口模块", value = "/wx/vtsp")
@RestController
@RequestMapping("/wx/vtsp")
public class WxApiController extends ApiBaseController
{
    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private IVtspActivityService vtspActivityService;

    @Autowired
    private IVtspPostService vtspPostService;

    @Autowired
    private IVtspSectorService vtspSectorService;

    @Autowired
    private IVtspAcademyService vtspAcademyService;

    @Autowired
    private IVtspVolunteerActivityService vtspVolunteerActivityService;

    @Autowired
    private IVtspEnrollService vtspEnrollService;

    @Autowired
    private IVtspAdviceService vtspAdviceService;

    @Autowired
    private IVtspAppraiseService vtspAppraiseService;

    @Autowired
    private IVtspFeedbackService vtspFeedbackService;

    @Autowired
    private IVtspAreasService vtspAreasService;

    @Autowired
    private IVtspVolunteerService vtspVolunteerService;

    @Autowired
    private IVtspMessageService vtspMessageService;

    /*-------------------------------------------[活动]---------------------------------------------------------*/

    /**
     * 判断志愿者是否进行过身份认证----------✔
     * @return 0授权过 1未授权
     */
    @ApiToken
    @GetMapping("/login")
    public ApiResult login(){
        //判断用户是否身份认证
        int row = 0;
        //当前用户在缓存中的数据
        UserModel user = getUser();
        Long oauthId = user.getOauthId();
        //根据授权id查询是否存在志愿者表中
        VtspVolunteer volunteer = vtspVolunteerService.selectVtspVolunteer(oauthId);
        //不在志愿者表中，第一次未身份认证
        if(volunteer == null){
            row = 1;
        }
        else{
            user.setUserId(volunteer.getVolunteerId());
            // 更新缓存
            reCacheUser(user);
        }
        return success(row);
    }

    /**
     * 志愿者身份认证----------✔
     */
    @ApiToken
    @GetMapping("/register")
    @ResponseBody
    public ApiResult register(String volunteerName, String volunteerEmail){
        VtspVolunteer vtspVolunteer = new VtspVolunteer();
        // 通过志愿者姓名和电子邮箱进行数据查询
        vtspVolunteer.setVolunteerName(volunteerName);
        vtspVolunteer.setVolunteerEmail(volunteerEmail);
        //志愿者状态为：（6：正式志愿者，才进行认证）
        vtspVolunteer.setMemberState(6);
        //查询志愿者信息列表
        VtspVolunteer volunteer = vtspVolunteerService.queryVtspVolunteer(vtspVolunteer);
        if (volunteer != null){
            //认证成功，将志愿者授权id写入该志愿者信息
            UserModel user = getUser();
            volunteer.setOauthId(user.getOauthId());
            //对志愿者表进行修改
            vtspVolunteerService.updateVtspVolunteer(volunteer);
            return ApiResult.success(1);//认证成功，返回1
        }else {
            return ApiResult.success(0);//认证失败，返回0
        }
    }

    /**
     * 申请加入志愿者----------✔
     */
    @ApiToken
    @PostMapping("/apply")
    public ApiResult apply(VtspVolunteer vtspVolunteer,Long areasId){
        //志愿者授权id
        UserModel user = getUser();
        vtspVolunteer.setOauthId(user.getOauthId());
        //出生日期的String Date类型转换
        vtspVolunteer.setVolunteerBirthday(DateUtils.dateTime("yyyy-MM-dd", vtspVolunteer.getVolunteerBirthdayStr()));
        //申请时，将志愿者状态设为：（1：申请成为志愿者，还未审批）
        vtspVolunteer.setMemberState(1);
        //新增志愿者信息
        int volunteer = vtspVolunteerService.insertVtspVolunteer(vtspVolunteer);
        return ApiResult.success(volunteer == 1 ? 1 : 0);//申请成功，返回1
    }

    /**
     * 志愿者学历----------✔
     */
    @ApiOperation(value = "志愿者学历")
    @ApiToken
    @GetMapping("/getDegree")
    public ApiResult getDegree() {
        //学历字典键值
        List<SysDictData> dicts = sysDictDataService.selectDictDataByType("degree");
        return success(dicts);
    }

    /**
     * 志愿者级时----------✔
     */
    @ApiOperation(value = "志愿者级时")
    @ApiToken
    @GetMapping("/getLevel")
    public ApiResult getLevel() {
        //级时字典键值
        List<SysDictData> dicts = sysDictDataService.selectDictDataByType("level");
        return success(dicts);
    }

    /**
     * 国家地区管理----------✔
     */
    @ApiOperation(value = "国家地区管理")
    @ApiToken
    @GetMapping("/getVtspAreasList")
    public ApiResult getVtspAreasList() {
        VtspAreas vtspAreas= new VtspAreas();
        //使用collection标签配置地区的List集合类型的属性，通过地区id匹配父id，查出国家相对应的省/州
        List<VtspAreas> list = vtspAreasService.selectVtspAreasList(vtspAreas);
        for (VtspAreas areas : list) {
            //打印桩，查数据
            System.err.println(areas);
        }
        return success(list);
    }

    /**
     * 国家地区管理-省/州----------✔
     */
/*    @ApiOperation(value = "国家地区管理")
    @ApiToken
    @GetMapping("/getVtspAreas")
    public ApiResult getVtspAreas(Long areasId) {
        VtspAreas areas = new VtspAreas();
        //根据前端传来的地区id，与父id对应，传回省/州
        areas.setParentId(areasId);
        List<VtspAreas> vtspAreasList= vtspAreasService.selectVtspAreasList(areas);
        return success(vtspAreasList);
    }*/

    /**
     * 部门信息----------✔
     */
    @ApiOperation(value = "部门信息")
    @ApiToken
    @GetMapping("/getVtspSectorList")
    public ApiResult getVtspSectorList(VtspSector vtspSector) {
        return success(vtspSectorService.selectVtspSectorList(vtspSector));
    }

    /**
     * 学院信息----------✔
     */
    @ApiOperation(value = "学院信息")
    @ApiToken
    @GetMapping("/getVtspAcademyList")
    public ApiResult getVtspAcademyList(VtspAcademy vtspAcademy) {
        return success(vtspAcademyService.selectVtspAcademyList(vtspAcademy));
    }

    /**
     * 查看活动列表----------✔
     */
    @ApiOperation(value = "查看活动列表")
    @ApiToken
    @GetMapping("/getVtspActivityList")
    public ApiResult getVtspActivityList() {
        //获取活动列表
        VtspActivity vtspActivity = new VtspActivity();
        List<VtspActivity> vtspActivityList = vtspActivityService.selectVtspActivityList(vtspActivity);
        //新建List数组，存通过报名表得出的报名人数与活动列表
        List<VtspActivity> list = new ArrayList<>();
        //获取报名列表
        VtspEnroll vtspEnroll= new VtspEnroll();
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
            for (VtspActivity activityList : vtspActivityList) {
                //活动报名人数
                if(activityList.getActivityState() == 1 || activityList.getActivityState() == 2 || activityList.getActivityState() == 6) {
                    long attendNum = 0;
                    for (VtspEnroll enrollList : vtspEnrollList) {
                        //活动表和报名表中活动id相对应
                        if(activityList.getActivityId().equals(enrollList.getActivityId())) {
                            attendNum++;
                        }
                    }
                    //在活动信息中加入活动报名人数
                    activityList.setAttendNum(attendNum);
                    //活动封面图
                    activityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), activityList.getActivityCover()));
                    list.add(activityList);
                }
            }
            return ApiResult.success(list);
    }

    /**
     * 根据活动Name查询活动信息-名称模糊查询----------✔
     */
    @ApiOperation(value = "根据活动Name查询活动信息")
    @ApiImplicitParam(value = "活动标识", name = "activityName")
    @ApiToken
    @GetMapping("/getVtspActivityByName")
    public ApiResult getVtspActivityByName(VtspActivity vtspActivity) {
        return success(vtspActivityService.selectVtspActivityList(vtspActivity));
    }

    /**
     * 查看活动详情----------✔
     * 报名状态（1：未审批；3：未通过；6：通过）
     */
    @ApiOperation(value = "查看活动详情")
    @ApiToken
    @GetMapping("/getVtspActivity")
    public ApiResult getVtspActivity(Long activityId) {
        //根据活动id查活动详情
        VtspActivity vtspActivity= vtspActivityService.queryActivityList(activityId);
        //查询岗位信息
        VtspPost vtspPost = new VtspPost();
        vtspPost.setActivityId(activityId);
        List<VtspPost> vtspPostList = vtspPostService.selectVtspPostList(vtspPost);
        //查询报名信息
        VtspEnroll vtspEnroll= new VtspEnroll();
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        for (VtspPost postList : vtspPostList) {
            //报名状态为（6：通过） && 岗位表和报名表的岗位id对应
            for (VtspEnroll enrollList : vtspEnrollList) {
                if (enrollList.getEnrollState() == 6 && enrollList.getPostId().equals(postList.getPostId())) {
                    postList.setPostEnrollNum(postList.getPostEnrollNum()+1);
                }
            }
        }
        //岗位报名人数添加到岗位列表
        vtspActivity.setPostList(vtspPostList);
        //活动封面图
        vtspActivity.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivity.getActivityCover()));
        return ApiResult.success(vtspActivity);
    }

    /**
     * 活动报名----------✔
     * 报名状态（1：未审批；3：未通过；6：通过）
     */
    @ApiOperation(value = "活动报名")
    @ApiToken
    @GetMapping("/getEnroll")
    public ApiResult getEnroll(Long activityId,Long postId) {
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(getUser().getUserId());//志愿者id
        vtspEnroll.setActivityId(activityId);//活动id
        vtspEnroll.setPostId(postId);//岗位id
        vtspEnroll.setEnrollState(1);//将报名状态设为（1：未审批）
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(getUserId());
        vtspEnroll.setCreateBy(vtspVolunteer.getVolunteerName());//创建者用志愿者姓名
        vtspEnroll.setCreateTime(DateUtils.getNowDate());//活动咨询创建时间=当前时间
        /*int enrollInformation = vtspEnrollService.insertVtspEnroll(vtspEnroll);*/
        /*return ApiResult.success(enrollInformation == 1 ? 1 : 0);*/
        //判断是否重复报名
        VtspEnroll enroll = vtspEnrollService.queryEnroll(vtspEnroll);
        if(enroll == null) {
            //新增报名信息
            return ApiResult.success(vtspEnrollService.insertVtspEnroll(vtspEnroll));//报名成功,返回 1
        } else{
            return error("您已报名，请勿再次提交");//重复报名
        }
    }

    /**
     * 判断是否关注----------✔
     */
    @ApiToken
    @GetMapping("/submitCollect")
    private ApiResult submitCollect(Long activityId) {
        //查询志愿者关注信息列表
        VtspVolunteerActivity collect = vtspVolunteerActivityService.selectVolunteerCollect(getUser().getUserId(),activityId);
            return ApiResult.success(collect != null ? 1 : 0);//已关注,返回1
    }

    /**
     * 志愿者添加关注活动----------✔
     */
    @ApiToken
    @PostMapping("/collectActivity")
    private ApiResult collectActivity(Long activityId) {
        VtspVolunteerActivity volunteerActivity = new VtspVolunteerActivity();
        volunteerActivity.setActivityId(activityId);//活动id
        volunteerActivity.setVolunteerId(getUser().getUserId());//志愿者id
        //新增关注信息
        int collect = vtspVolunteerActivityService.insertVtspVolunteerActivity(volunteerActivity);
        return ApiResult.success(collect);
    }

    /**
     * 取消关注----------✔
     */
    @ApiOperation(value = "取消关注")
    @ApiToken
    @PostMapping("/cancelCollect")
    public ApiResult cancelCollect(Long activityId) {
        //删除志愿者关注活动信息
        int cancel = vtspVolunteerActivityService.deleteCollectById(getUser().getUserId(),activityId);
        return success(cancel);
    }

    /**
     * 新增活动咨询----------✔
     */
    @ApiToken
    @GetMapping("/submitAdvice")
    public ApiResult submitAdvice(Long activityId,String adviceContent) {
        VtspAdvice vtspAdvice = new VtspAdvice();
        vtspAdvice.setActivityId(activityId);//咨询的活动id
        vtspAdvice.setAdviceContent(adviceContent);//咨询内容
        vtspAdvice.setVolunteerId(getUser().getUserId());//志愿者id
        vtspAdvice.setState(0);//活动咨询字典状态：（0：未解决）
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(getUserId());
        vtspAdvice.setCreateBy(vtspVolunteer.getVolunteerName());//创建者用志愿者姓名
        vtspAdvice.setCreateTime(DateUtils.getNowDate());//活动咨询创建时间=当前时间
        //新增活动咨询
        int advice = vtspAdviceService.insertVtspAdvice(vtspAdvice);
        return ApiResult.success(advice);
    }

    /*-------------------------------------------[我的]---------------------------------------------------------*/

    /**
     * 查看志愿者个人信息----------✔
     * @return	志愿者信息
     */
    @ApiOperation(value = "查看志愿者个人信息")
    @ApiToken
    @GetMapping("/myInformation")
    private ApiResult myInformation(){
        //通过志愿者id查询志愿者信息
        UserModel user = getUser();
        VtspVolunteer volunteerInformation = vtspVolunteerService.selectVtspVolunteerById(user.getUserId());
        //查询志愿者表中志愿者所在地的地区id
        VtspAreas vtspAreas = vtspAreasService.queryAreasById(Long.parseLong(volunteerInformation.getVolunteerArea()));
        //获取地区的父id
        VtspAreas parentVtspAreas = vtspAreasService.queryAreasById(vtspAreas.getParentId());
        //根据父id得到对应的国家名
        volunteerInformation.setVolunteerCountry(parentVtspAreas.getAreasName());//国家名
        //根据志愿者所在地的地区id得到对应的省级名
        volunteerInformation.setVolunteerProvince(vtspAreas.getAreasName());//省份名
        double score = 0;//志愿者综合评价
        int count = 0;//参与活动已评价次数
        //查询志愿者在评价表中的参与评分
        List<VtspAppraise> vtspAppraise = vtspAppraiseService.selectVtspAppraiseScore(user.getUserId());
        for (VtspAppraise appraiseList: vtspAppraise){
            //评价不为空，评价状态为（2：已评价）
            if (vtspAppraise != null) {
                //将参与评分相加
                score += appraiseList.getAppraiseScore();
                count++;
            }
        }
        if (count != 0) {
            //总评分除次数得出平均分=综合评价
            volunteerInformation.setVolunteerScore(score/count);
        }
        return success(volunteerInformation);
    }

    /**
     * 修改志愿者个人信息----------✔
     */
    @ApiToken
    @GetMapping("/modifyMyInformation")
    public ApiResult modifyMyInformation() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspVolunteer vtspVolunteer = new VtspVolunteer();
        vtspVolunteer.setVolunteerId(user.getUserId());
        //修改志愿者信息
        int volunteer = vtspVolunteerService.updateVtspVolunteer(vtspVolunteer);
        return ApiResult.success(volunteer);
    }

    /**
     * 查看我的志愿服务记录----------✔
     * 报名状态：（1：未审批；3：未通过；6：通过）
     * 评分、参与时长
     */
    @ApiOperation(value = "查看我的志愿服务记录")
    @ApiToken
    @GetMapping("/serviceRecord")
    public ApiResult serviceRecord() {
        //获取志愿者信息
        UserModel user = getUser();
        //查询志愿服务记录页面的相关信息,报名状态为（6：通过），才算做记录
        List<VtspEnrollVo> vtspEnrollVoList = vtspEnrollService.selectServiceRecord(user.getUserId());
        //志愿者累计活动时长
        int totalTime = 0;
        for (VtspEnrollVo list: vtspEnrollVoList) {
            //将参与时长相加等于服务总时长
            totalTime += list.getAppraiseTime();
        }
        ServiceRecord serviceRecord = new ServiceRecord();
        serviceRecord.setActivityCount(vtspEnrollVoList.size());//参加活动次数
        serviceRecord.setTotalTime((long) totalTime);//累计活动时长
        serviceRecord.setVtspEnrollVos(vtspEnrollVoList);//活动信息
        return ApiResult.success(serviceRecord);
    }

    /**
     * 查看我的参与列表-全部----------✔
     * 报名状态：（1：未审批；3：未通过；6：通过）
     */
    @ApiOperation(value = "查看我的参与列表")
    @ApiToken
    @GetMapping("/myAttends")
    public ApiResult myAttends() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspVolunteer volunteer = vtspVolunteerService.selectVtspVolunteerByOauthId(user.getOauthId());
        //通过报名表中志愿者id查询
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(volunteer.getVolunteerId());
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        //新建List数组添加活动列表和报名人数
        List<VtspActivity> attendList = new ArrayList<>();
        for (VtspEnroll list: vtspEnrollList) {
            //根据报名信息获取活动id
            VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(list.getActivityId());
            //添加志愿者报名状态
            vtspActivity.setEnrollState(list.getEnrollState());
            //将查询到的志愿者参与的活动信息通过活动id存入新建的数组中
            attendList.add(vtspActivity);
        }
        for (VtspActivity vtspActivityList : attendList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll enrollList : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(enrollList.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return success(attendList);
    }

    /**
     * 查看我的参与列表-审核中-----------✔
     * 报名状态：（1：审核中；3：未通过；6：通过）
     */
    @ApiToken
    @GetMapping("/myAttendCheck")
    public ApiResult myAttendCheck() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(user.getUserId());
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        //新建List数组添加活动列表和报名人数
        List<VtspActivity> attendList = new ArrayList<>();
        for (VtspEnroll enrollList : vtspEnrollList) {
            //报名状态为（1：审核中）
            if (enrollList.getEnrollState() == 1) {
                //根据报名信息获取活动id
                VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(enrollList.getActivityId());
                //添加志愿者报名状态
                vtspActivity.setEnrollState(enrollList.getEnrollState());
                attendList.add(vtspActivity);
            }
        }
        for (VtspActivity vtspActivityList : attendList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll list : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(list.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return ApiResult.success(attendList);
    }

    /**
     * 查看我的参与列表-未通过-----------✔
     * 报名状态：（1：未审批；3：未通过；6：通过）
     */
    @ApiToken
    @GetMapping("/myAttendNotPass")
    public ApiResult myAttendNotPass() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(user.getUserId());
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        //新建List数组添加活动列表和报名人数
        List<VtspActivity> attendList = new ArrayList<>();
        for (VtspEnroll enrollList : vtspEnrollList) {
            //报名状态为（3：未通过）
            if (enrollList.getEnrollState() == 3) {
                //根据报名信息获取活动id
                VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(enrollList.getActivityId());
                //添加志愿者报名状态
                vtspActivity.setEnrollState(enrollList.getEnrollState());
                attendList.add(vtspActivity);
            }
        }
        for (VtspActivity vtspActivityList : attendList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll list : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(list.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return ApiResult.success(attendList);
    }

    /**
     * 查看我的参与列表-通过-----------✔
     * 报名状态：（1：未审批；3：未通过；6：通过）
     */
    @ApiToken
    @GetMapping("/myAttendPass")
    public ApiResult myAttendPass() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(user.getUserId());
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        //新建List数组添加活动列表和报名人数
        List<VtspActivity> attendList = new ArrayList<>();
        for (VtspEnroll enrollList : vtspEnrollList) {
            //报名状态为（6：通过）
            if (enrollList.getEnrollState() == 6) {
                //根据报名信息获取活动id
                VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(enrollList.getActivityId());
                //添加志愿者报名状态
                vtspActivity.setEnrollState(enrollList.getEnrollState());
                attendList.add(vtspActivity);
            }
        }
        for (VtspActivity vtspActivityList : attendList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll list : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(list.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return ApiResult.success(attendList);
    }

    /**
     * 查看我的参与列表-已结束-----------✔
     * 报名状态：（1：未审批；3：未通过；6：通过）
     */
    @ApiToken
    @GetMapping("/myAttendEnd")
    public ApiResult myAttendEnd() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspEnroll vtspEnroll = new VtspEnroll();
        vtspEnroll.setVolunteerId(user.getUserId());
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(vtspEnroll);
        //新建List数组添加活动列表和报名人数
        List<VtspActivity> attendList = new ArrayList<>();
        for (VtspEnroll enrollList : vtspEnrollList) {
            //报名状态为（6：通过）
            if (enrollList.getEnrollState() == 6) {
                //根据报名信息获取活动id
                VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(enrollList.getActivityId());
                //活动状态为：（6：已结束）
                if (vtspActivity.getActivityState() == 6) {
                    VtspAppraise vtspAppraise = vtspAppraiseService.queryVtspAppraiseById(enrollList.getEnrollId());
                    if (vtspAppraise != null) {
                        //评论状态：（2：已评价）
                        vtspActivity.setAppraiseState(2);
                    }
                    else {
                        //评价状态：（1：未评价）
                        vtspActivity.setAppraiseState(1);
                    }
                    attendList.add(vtspActivity);
                }
            }
        }
        for (VtspActivity vtspActivityList : attendList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll list : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(list.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return ApiResult.success(attendList);
    }

    /**
     * 新增评价活动----------✔
     */
    @ApiToken
    @GetMapping("/submitAppraise")
    public ApiResult submitAppraise(Long activityId,String appraiseContent) {
        VtspAppraise vtspAppraise = new VtspAppraise();
        vtspAppraise.setAppraiseContent(appraiseContent);//评价内容
        vtspAppraise.setAppraiseState(2);//活动评价状态：（2：已评价）
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(getUserId());
        VtspEnroll vtspEnroll = vtspEnrollService.queryVtspEnrollById(getUser().getUserId(),activityId);
        vtspAppraise.setEnrollId(vtspEnroll.getEnrollId());
        vtspAppraise.setCreateBy(vtspVolunteer.getVolunteerName());//创建者用志愿者姓名
        vtspAppraise.setCreateTime(DateUtils.getNowDate());//活动评价创建时间=当前时间
        //新增活动评价
        int appraise = vtspAppraiseService.insertVtspAppraise(vtspAppraise);
        return ApiResult.success(appraise);
    }

    /**
     * 查看我的关注列表----------✔
     */
    @ApiOperation(value = "查看我的关注列表")
    @ApiToken
    @GetMapping("/myCollections")
    public ApiResult myCollections() {
        //获取志愿者信息
        UserModel user = getUser();
        VtspVolunteer volunteer = vtspVolunteerService.selectVtspVolunteerByOauthId(user.getOauthId());
        //通过志愿者关注表中志愿者id查询
        VtspVolunteerActivity vtspVolunteerActivity = new VtspVolunteerActivity();
        vtspVolunteerActivity.setVolunteerId(volunteer.getVolunteerId());
        List<VtspVolunteerActivity> vtspVolunteerActivityList = vtspVolunteerActivityService.selectVtspVolunteerActivityList(vtspVolunteerActivity);
        //新建数组添加活动列表
        List<VtspActivity> activityList = new ArrayList<>();
        for (VtspVolunteerActivity list: vtspVolunteerActivityList) {
            //将查询到的志愿者关注的活动信息通过活动id存入新建的数组中
            activityList.add(vtspActivityService.selectVtspActivityById(list.getActivityId()));
        }
        List<VtspEnroll> vtspEnrollList = vtspEnrollService.selectVtspEnrollList(new VtspEnroll());
        for (VtspActivity vtspActivityList : activityList) {
            //活动报名人数
            long attendNum = 0;
            for (VtspEnroll enrollList : vtspEnrollList) {
                if (vtspActivityList.getActivityId().equals(enrollList.getActivityId())) {
                    attendNum++;
                }
            }
            vtspActivityList.setAttendNum(attendNum);
            //活动封面图
            vtspActivityList.setActivityCover(String.format("%s%s", Global.getWebUrl(), vtspActivityList.getActivityCover()));
        }
        return success(activityList);
    }

    /**
     * 新增问题反馈----------✔
     */
    @ApiToken
    @GetMapping("/submitFeedback")
    private ApiResult submitFeedback(String feedbackContent) {
        VtspFeedback vtspFeedback = new VtspFeedback();
        vtspFeedback.setFeedbackContent(feedbackContent);//反馈内容
        vtspFeedback.setVolunteerId(getUser().getUserId());//志愿者id
        vtspFeedback.setState(0);//问题反馈状态：（0：未解决）
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(getUserId());
        vtspFeedback.setCreateBy(vtspVolunteer.getVolunteerName());//创建者用志愿者姓名
        vtspFeedback.setCreateTime(DateUtils.getNowDate());//活动评价创建时间=当前时间
        //新增问题反馈
        int feedback = vtspFeedbackService.insertVtspFeedback(vtspFeedback);
        return ApiResult.success(feedback);
    }

    /*-------------------------------------------[消息]---------------------------------------------------------*/

    /**
     * 查询消息列表----------✔
     * 状态：1：已读
     */
    @ApiOperation(value = "查询消息列表")
    @ApiToken
    @GetMapping("/myMessages")
    public ApiResult myMessages(Long messageId) {
        List<VtspMessage> list = vtspMessageService.selectVtspMessageListById(getUser().getUserId());
        for (VtspMessage messages : list) {
            //将3-活动招募，4-活动邀请，5-活动报名设为已读
            if (messages.getMessageClassify() != 1 && messages.getMessageClassify() != 2)
            {
                VtspMessage vtspMessage = new VtspMessage();
                vtspMessage.setMessageId(messageId);//消息id
                messages.setState(1);//消息状态（1：已读）
                //修改消息信息
                vtspMessageService.updateVtspMessage(messages);
            }
        }
        //反转列表顺序
        Collections.reverse(list);
        return ApiResult.success(list);
    }

    /**
     * 消息列表——活动咨询----------✔
     * 状态：1：已读
     */
    @ApiOperation(value = "消息列表——活动咨询")
    @ApiToken
    @GetMapping("/activityConsults")
    public ApiResult activityConsults(Long messageId) {
        VtspMessage message = vtspMessageService.selectVtspMessageById(messageId);
        VtspAdvice vtspAdvice = vtspAdviceService.selectVtspAdviceById(message.getBizId());//查询咨询Id
        message.setBizContent(vtspAdvice.getAdviceContent());//提交活动咨询的内容
        vtspAdvice.setState(1);//将活动咨询的状态设为：（1:已读）
        vtspAdviceService.updateVtspAdvice(vtspAdvice);//修改咨询表中的状态
        VtspActivity vtspActivity = vtspActivityService.selectVtspActivityById(vtspAdvice.getActivityId());
        message.setActivityName(vtspActivity.getActivityName());//活动名称
        message.setBizTime(vtspAdvice.getCreateTime());//提交活动咨询的时间
        message.setState(1);//消息状态（1：已读）
        //修改消息信息
        vtspMessageService.updateVtspMessage(message);
        message.setMessageContent(vtspAdvice.getAdviceAnswer());//平台回复内容
        message.setAnswerTime(message.getCreateTime());//平台回复时间
        return ApiResult.success(message);
    }

    /**
     * 消息列表——问题反馈----------✔
     * 状态：1：已读
     */
    @ApiOperation(value = "消息列表——问题反馈")
    @ApiToken
    @GetMapping("/problemFeedback")
    public ApiResult problemFeedback(Long messageId) {
        VtspMessage message = vtspMessageService.selectVtspMessageById(messageId);
        VtspFeedback vtspFeedback = vtspFeedbackService.selectVtspFeedbackById(message.getBizId());
        message.setBizContent(vtspFeedback.getFeedbackContent());//提交问题反馈的内容
        vtspFeedback.setState(1);//将问题反馈的状态设为：（1:已读）
        vtspFeedbackService.updateVtspFeedback(vtspFeedback);
        message.setBizTime(vtspFeedback.getCreateTime());//提交问题反馈的时间
        message.setState(1);//消息状态（1：已读）
        //修改消息信息
        vtspMessageService.updateVtspMessage(message);
        message.setMessageContent(vtspFeedback.getFeedbackAnswer());//平台回复内容
        message.setAnswerTime(message.getCreateTime());//平台回复时间
        return ApiResult.success(message);
    }

}
